package crack;

import org.junit.Test;

/**
 * @author fance
 * @date 18-3-31 上午6:50
 */
public class ArrayAndString {

    public boolean isUnqiueChar(String s) {
        // 漏了边界值
        if (s.length() > 256) {
            return false;
        }
        boolean[] char_set = new boolean[256];
        for (int i = 0; i < s.length(); i++) {
            int val = s.charAt(i);
            if (char_set[val]) {
                return false;
            }
            char_set[val] = true;
        }
        return true;
        /* 排序
        char[] arr = s.toCharArray();
        Arrays.sort(arr);
        for (int i = 0; i < arr.length - 1; i++) {
            if (arr[i] == arr[i + 1]) {
                return false;
            }
        }
        return true;*/


    }

    public String reverse(String s) {
        return new StringBuilder(s).reverse().toString();
    }

    public boolean isAnagram(String s1, String s2) {
        // 1. 排序 2.统计字符个数必须完全一样
            if (s1.length() != s2.length()) {
            return false;
        }
        int[] tmp = new int[256];
        for (int i = 0; i < s1.length(); i++) {
            int val = s1.charAt(i);
            tmp[val]++;
        }
        for (int i = 0; i < s2.length(); i++) {
            int val = s2.charAt(i);
            if (tmp[val]-- < 0) {
                return false;
            }
        }
        return true;
    }

    public String compress(String s) {
        int size = cntCompression(s);
        if (size >= s.length()) {
            return s;
        }
        StringBuilder sb = new StringBuilder();
        char last = s.charAt(0);
        int cnt = 1;
        for (int i = 1; i < s.length(); i++) {
            if (s.charAt(i) == last) {
                cnt++;
            } else {
                sb.append(last);
                sb.append(cnt);
                last = s.charAt(i);
                cnt = 1;
            }
        }
        sb.append(last);
        sb.append(cnt);
        return sb.toString();
    }
    private int cntCompression(String s) {
        if (s == null || s.isEmpty()) {
            return 0;
        }
        char last = s.charAt(0);
        int size = 0;
        int cnt = 1;
        for (int i = 1; i < s.length(); i++) {
            if (s.charAt(i) == last) {
                cnt++;
            } else {
                last = s.charAt(i);
                size += 1 + String.valueOf(cnt).length();
                cnt = 1;
            }
        }
        size += 1 + String.valueOf(cnt).length();
        return size;
    }

    public void rotate(int[][] m, int n) {
        for (int i = 0; i < n / 2; i++) {
            int first = i;
            int last = n - 1 - i;
            for (int j = first; j < last; j++) {
                int offset = j - first;
                int top = m[first][j];
                m[first][j] = m[last - offset][first];
                m[last - offset][first] = m[last][last - offset];
                m[last][last - offset] = m[j][last];
                m[j][last] = top;
            }
        }
    }

    public void setZero(int[][] m) {
        boolean[] row = new boolean[m.length];
        boolean[] col = new boolean[m[0].length];

        for (int i = 0; i < m.length; i++) {
            for (int j = 0; j < m[0].length; j++) {
                if (m[i][j] == 0) {
                    row[i] = true;
                    col[j] = true;
                }
            }
        }
        for (int i = 0; i < m.length; i++) {
            for (int j = 0; j < m[0].length; j++) {
                if (row[i] || col[j]) {
                    m[i][j] = 0;
                }
            }
        }
    }

    /**
     * 总结思路
     */
    public void mergeThink() {
        // 空格替换 库函数
        // 字符串是否旋转而成,  s1+ s1  用kmp 找S2
    }
    @Test
    public void test() {

        System.out.println(compress("aabbbcc"));
    }
}
